package orbitPrototype.loaders;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import orbitPrototype.Settings;
import orbitPrototype.Util;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLLoader
{
	private static HashMap<String, Document> _docList = new HashMap<String, Document>();
	
	private static final String FILE_ENDING = ".xml";
	
	public XMLLoader()
	{
		
	}
	
	public static Document getXML(String document)
	{
		Document xmlDocument = null;
		
		if((xmlDocument = _docList.get(document)) != null)
		{
			return xmlDocument;
		}
		else
		{
			try
			{
				String path = Settings.PATH_XML + document + FILE_ENDING;
				
				Util.output("Loading XML: " + path + "...");
				
				File XMLFile = new File(path);
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder;
				dBuilder = dbFactory.newDocumentBuilder();
				xmlDocument = dBuilder.parse(XMLFile);
				
				_docList.put(document, xmlDocument);
			}
			catch (ParserConfigurationException e)
			{
				e.printStackTrace();
			} 
			catch (SAXException e)
			{
				e.printStackTrace();
			}
			catch (FileNotFoundException e)
			{
				Util.error("File not found " + Settings.PATH_XML + document + FILE_ENDING, e.getMessage());
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			} 
			
			return xmlDocument;
		}
	}
	
	public static Element getXMLElementByUniqueName(String document, String uniqueName)
	{
		Document xmlDocument = getXML(document);
		
		return xmlDocument.getElementById(uniqueName);
	}
	
	public static Boolean getXMLElementByUniqueNameExists(String document, String uniqueName)
	{
		Element xmlElement = getXMLElementByUniqueName(document, uniqueName);
		
		if(xmlElement != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static String getElementValue(String tag, Element element)
	{
		NodeList nodeList = element.getChildNodes();
		
		if(nodeList.getLength() > 0)
		{
			for(int i = 0; i < nodeList.getLength(); i++)
			{
				if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE
						&& tag.equals(nodeList.item(i).getNodeName()))
				{
					return nodeList.item(i).getTextContent();
				}
			}
			
			return "";
		}
		else
		{
			return "";
		}
	}
	
	public static int getElementIntValue(String tag, Element element)
	{
		return Integer.parseInt(getElementValue(tag, element));
	}
	
	public static boolean getElementBooleanValue(String tag, Element element)
	{
		String value = getElementValue(tag, element);
		
		if(value.equals("false"))
		{
			return false;
		}
		else if(value.equals("true"))
		{
			return true;
		}
		else
		{
			Util.error("Can't resolve tag " + tag + " to boolean in element " + element.getTagName());
			return false;
		}
	}
	
	public static boolean getAttributeBooleanValue(String attribute, Element element)
	{
		String value = element.getAttribute(attribute);
		
		if(value.equals("false"))
		{
			return false;
		}
		else if(value.equals("true"))
		{
			return true;
		}
		else
		{
			Util.error("Can't resolve attribute " + attribute + " to boolean in element " + element.getTagName());
			return false;
		}
	}
}
